package com.example.security.dto;

import com.example.sys.org.po.SysRole;
import com.example.sys.org.po.SysUser;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
public class UserAuthInfo implements Serializable {

    private SysUser sysUser;

    private String name;

    private String avatar;

    private Set<String> roles = new HashSet<>();

    private List<SysRole> sysRoles = new ArrayList<>();

    private List<SysRole> openRoles = new ArrayList<>();

    public UserAuthInfo(SysUser sysUser, List<SysRole> sysRoles, List<SysRole> openRoles) {
        this.sysUser = sysUser;
        this.sysRoles = sysRoles;
        this.openRoles = openRoles;
        if (sysUser != null) {
            this.name = sysUser.getName();
            this.avatar = sysUser.getAvatar();
        }
        if (!sysRoles.isEmpty()) {
            for (SysRole sysRole : sysRoles) {
                this.roles.add(sysRole.getCode());
            }
        }
        if (!openRoles.isEmpty()) {
            for (SysRole sysRole : openRoles) {
                this.roles.add(sysRole.getCode());
            }
        }
    }
}
